package com.wenli.graduate2022.wenliGreenShop.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wenli.graduate2022.wenliGreenShop.entity.Category;
import com.wenli.graduate2022.wenliGreenShop.entity.Property;
import com.wenli.graduate2022.wenliGreenShop.service.servDao.PropertyService;
import javafx.beans.property.ListProperty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.List;
import java.util.logging.Logger;

/**
 * @author HuangHai
 * @date 2021/10/8 21:31
 */
@RestController
public class PropertyController {
    @Autowired
    PropertyService propertyService;

    //查询所有属性
    @GetMapping("property")
    public List<Property> findAll() {
        System.out.println("查询所有方法运行");
        return propertyService.findAll();
    }

    /**
     * 这里的IPage类与Page类都是mybatisPlus给我们自动生成的，只要输入当前页面数与该页面的数据条数，他就会自动帮我们算出需要的数据返回给我们
     * 分页查询属性
     *
     * @param page
     * @param size
     * @return
     */
    @GetMapping("/property/{page}/{size}")
    public IPage<Property> findByPage(@PathVariable("page") Integer page, @PathVariable("size") Integer size) {
        Page<Property> propertyPage = new Page<Property>(page, size);
        IPage<Property> propertyIPage = propertyService.findByPage(propertyPage);
        System.out.println("打印页面：" + propertyPage.toString());
        System.out.println("分页查询所有属性：" + propertyIPage.toString());
        return propertyIPage;
    }

    @GetMapping("/property/{categoryId}")
    public List<Property> findByCategoryId(@PathVariable("categoryId") Integer id) {
        System.out.println("查询具体属性方法运行");
        return propertyService.findByCategoryId(id);
    }

    @PostMapping("/property")
    public int insert(@RequestBody Property property){
        System.out.println("插入方法运行");
        return propertyService.insert(property);
    }

    @PutMapping("/property")
    public int update(@RequestBody Property property){
        System.out.println("修改方法运行");
        return propertyService.update(property);
    }

    @DeleteMapping("/property/{id}")
    public int delete(@PathVariable("id") Integer id){
        System.out.println("删除方法运行");
        return propertyService.delete(id);
    }

    @GetMapping("/propertyByName/{name}")  //这里字符串也可以用restful风格
    public List<Property> findByName(@PathVariable("name") String name){
        System.out.println("模糊查询方法运行");
        return propertyService.findByName(name);
    }

    @PostMapping("/propertyByCategoryName")  //根据某个种类的种类名查询它的所有属性
    public List<Property> selectByCategoryName(@RequestBody Category category){
        Logger logger=Logger.getLogger(PropertyController.class.getName());
        logger.info(category.getCategoryName());
        return propertyService.selectByCategoryName(category.getCategoryName());
    }
}
